<?php

namespace app\controller;

use app\BaseController;
use app\model\BocEvaluate;
use app\model\BocSupplier;

class Evaluate extends BaseController
{
    /*
     * 供应商列表
     */
    public function supplier()
    {
        $page = $this->request->get('page');
        $supplier_m = new BocEvaluate();
        $res = $supplier_m->paginate(5, false, [], $page);
        $data = $res->toArray();

        return json(['code' => 200, 'data' => $data]);
    }

    public function get_supplier_list()
    {
        $supplier_m = new BocSupplier();
        $res = $supplier_m->get_list();

        return result($res);
    }

    public function evaluateadd()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            try {
                $supplier_m = new BocSupplier();
                $gys_list = $supplier_m->where(['gys_order' => $post['gys_order']])->find();
                $fenshu = $post['zl'] = $post['jg'] + $post['jh'] + $post['fw'];
                $level = 'E';
                switch ($fenshu) {
                    case $fenshu > 20:
                        $level = 'A';
                        break;
                    case $fenshu > 15:
                        $level = 'B';
                        break;
                    case $fenshu > 10:
                        $level = 'C';
                        break;
                    case $fenshu > 5:
                        $level = 'D';
                        break;
                }
                $post['level'] = $level;
                $post['gys_name'] = $gys_list['gys_name'];
                $evaluate_m = new BocEvaluate();
                $save = $evaluate_m->save($post);
                if (!$save) {
                    throw new \Exception($save);
                }

                $res = $supplier_m->where(['gys_order' => $post['gys_order'], 'state' => 1, 'is_deleted' => 1])->save(['level' => $level]);
                if (!$res) {
                    throw new \Exception($res);
                }

                return result($res);
            } catch (\Exception $e) {
                return json(['code' => 400, 'msg' => $e->getMessage()]);
            }
        }
    }
}
